//
//  RegisterViewController.m
//  MyPizzaMobile
//
//  Created by Stanislav on 2/15/13.
//  Copyright (c) 2013 Stanislav. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property(readwrite) RequestManager * requestManager;
@end

@implementation RegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.requestManager = [RequestManager requestManagerInstance];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setUsernameTextField:nil];
    [self setEmailTextField:nil];
    [self setPasswordTextField:nil];
    [self setAddressTextField:nil];
    [self setPhoneTextField:nil];
    [self setOrdersAddressTextField:nil];
    [super viewDidUnload];
}

- (IBAction)nextFieldPressed:(UITextField *)sender {
    if(sender == self.usernameTextField)
    {
       [self.passwordTextField becomeFirstResponder];
    }
    else if(sender == self.passwordTextField)
    {
        [self.addressTextField becomeFirstResponder];
    }
    else if (sender == self.addressTextField)
    {
        [self.phoneTextField becomeFirstResponder];
    }
    else if(sender == self.phoneTextField)
    {
        [self.ordersAddressTextField becomeFirstResponder];
    }
    else if(sender == self.ordersAddressTextField)
    {
        [self.ordersAddressTextField resignFirstResponder];
        [self sendRegisterInfo];
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.usernameTextField resignFirstResponder];
        [self.passwordTextField resignFirstResponder];
        [self.addressTextField resignFirstResponder];
        [self.phoneTextField resignFirstResponder];
        [self.ordersAddressTextField resignFirstResponder];
        
    }
}
- (void) sendRegisterInfo
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    [dict setObject:self.usernameTextField.text forKey:@"username"];
    [dict setObject:self.passwordTextField.text forKey:@"password"];
    [dict setObject:self.addressTextField.text forKey:@"address"];
    [dict setObject:self.phoneTextField.text forKey:@"phone"];
    [dict setObject:self.ordersAddressTextField.text forKey:@"ordersAddress"];
    if([self.requestManager sendRegistrationWithDict:[dict copy]])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (IBAction)dissmissKeyboard:(UITextField *)sender
{
    [sender resignFirstResponder];
}
@end
